package cn.ldf.keepaccounts.utils;

import cn.hutool.core.util.StrUtil;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.function.Supplier;

/**
 * @author ldf
 */
public class ExceptionUtil {
    /**
     * 获取异常堆栈信息
     *
     * @param exception 异常
     * @return 异常堆栈信息
     */
    public static String getStackTraceAsString(Throwable exception) {
        StringWriter sw = new StringWriter();
        exception.printStackTrace(new PrintWriter(sw));
        return sw.toString();
    }

    /**
     * 抛出异常处理
     * @param supplier boolean条件
     * @param msgTemplate 异常消息模版
     * @param args 异常消息模版参数
     */
    public static void throwMsg(Supplier<Boolean> supplier, String msgTemplate,Object... args){
        if (supplier.get()){
            msgTemplate = StrUtil.format(msgTemplate, args);
            throw new RuntimeException(msgTemplate);
        }
    }

}
